﻿using System;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Routing;

namespace Motar.Web.Mvc
{
    
    /// <summary>
    /// 
    /// </summary>
    public static class AreaRegistrationContextExtensions
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        public static void MapLocalizedRoute(this AreaRegistrationContext context, string name, string url)
        {
            MapLocalizedRoute(context, name, url, nr => { } /* action */);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="action"></param>
        public static void MapLocalizedRoute(this AreaRegistrationContext context, string name, string url, Action<Route> action)
        {
            MapLocalizedRoute(context, name, url, null /* defaults */, (object)null /* constraints */, action);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="defaults"></param>
        public static void MapLocalizedRoute(this AreaRegistrationContext context, string name, string url, object defaults)
        {
            MapLocalizedRoute(context, name, url, defaults, nr => { } /* action */);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="defaults"></param>
        /// <param name="action"></param>
        public static void MapLocalizedRoute(this AreaRegistrationContext context, string name, string url, object defaults, Action<Route> action)
        {
            MapLocalizedRoute(context, name, url, defaults, (object)null /* constraints */, action);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="namespaces"></param>
        public static void MapLocalizedRoute(this AreaRegistrationContext context, string name, string url, string[] namespaces)
        {
            MapLocalizedRoute(context, name, url, namespaces, nr => { } /* action */);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="namespaces"></param>
        /// <param name="action"></param>
        public static void MapLocalizedRoute(this AreaRegistrationContext context, string name, string url, string[] namespaces, Action<Route> action)
        {
            MapLocalizedRoute(context, name, url, null /* defaults */, null /* constraints */, namespaces, action);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="defaults"></param>
        /// <param name="constraints"></param>
        public static void MapLocalizedRoute(this AreaRegistrationContext context, string name, string url, object defaults, object constraints)
        {
            MapLocalizedRoute(context, name, url, defaults, constraints, nr => { } /* action */);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="defaults"></param>
        /// <param name="constraints"></param>
        /// <param name="action"></param>
        public static void MapLocalizedRoute(this AreaRegistrationContext context, string name, string url, object defaults, object constraints, Action<Route> action)
        {
            MapLocalizedRoute(context, name, url, defaults, constraints, null /* namespaces */, action);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="defaults"></param>
        /// <param name="namespaces"></param>
        public static void MapLocalizedRoute(this AreaRegistrationContext context, string name, string url, object defaults, string[] namespaces)
        {
            MapLocalizedRoute(context, name, url, defaults, namespaces, nr => { } /* action */);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="defaults"></param>
        /// <param name="namespaces"></param>
        /// <param name="action"></param>
        public static void MapLocalizedRoute(this AreaRegistrationContext context, string name, string url, object defaults, string[] namespaces, Action<Route> action)
        {
            MapLocalizedRoute(context, name, url, defaults, null /* constraints */, namespaces, action);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="defaults"></param>
        /// <param name="constraints"></param>
        /// <param name="namespaces"></param>
        public static void MapLocalizedRoute(this AreaRegistrationContext context, string name, string url, object defaults, object constraints, string[] namespaces)
        {
            MapLocalizedRoute(context, name, url, defaults, constraints, namespaces, nr => { } /* action */);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="defaults"></param>
        /// <param name="constraints"></param>
        /// <param name="namespaces"></param>
        /// <param name="action"></param>
        public static void MapLocalizedRoute(this AreaRegistrationContext context, string name, string url, object defaults, object constraints, string[] namespaces, Action<Route> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            Route route = context.MapRoute(string.Format("{0}_localized", name), string.Format("{{{0}}}/{1}", LocalizeAttributeBase.LocaleRouteSegment, url), defaults, constraints, namespaces);

            action.Invoke(route);

            route = context.MapRoute(name, url, defaults, constraints, namespaces);

            action.Invoke(route);
        }
    }
}
